﻿<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@page import="com.zaofan.bean.Admin" %>
<%@page import="com.zaofan.bean.AdminType" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
    String contextPath = request.getContextPath();
    Admin admin = (Admin) session.getAttribute("admin");
    if (admin == null) {
        response.sendRedirect(contextPath + "/login.jsp?state=" + "session-invalid");
        return;
    }
    if (admin.getType() != AdminType.admin.ordinal() && admin.getType() != 4) {
        response.sendRedirect(contextPath + "/login.jsp?state=" + "session-invalid");
        return;
    }
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>刮刮乐管理</title>
    <link href="<%=contextPath%>/admin/css/base.css" rel="stylesheet"/>
    <link href="<%=contextPath%>/admin/assets/css/bootstrap.min.css"
          rel="stylesheet"/>
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/font-awesome.min.css"/>
    <!--[if IE 7]>
    <link rel="stylesheet" href="<%=contextPath%>/admin/assets/css/font-awesome-ie7.min.css"/>
    <![endif]-->
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/ace.min.css"/>
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/ace-rtl.min.css"/>
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/ace-skins.min.css"/>
    <!--[if lte IE 8]>
    <link rel="stylesheet" href="<%=contextPath%>/admin/assets/css/ace-ie.min.css"/>
    <![endif]-->
    <script src="<%=contextPath%>/admin/assets/js/ace-extra.min.js"></script>
    <!--[if lt IE 9]>
    <script src="<%=contextPath%>/admin/assets/js/html5shiv.js"></script>
    <script src="<%=contextPath%>/admin/assets/js/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/jquery-ui-1.10.3.custom.min.css"/>
    <link rel="stylesheet"
          href="<%=contextPath%>/admin/assets/css/chosen.css"/>
    <script type="text/javascript">
        var contextPath = '<%=contextPath%>';
    </script>
</head>
<body style="background-color: #fff;">
<div class="breadcrumbs" id="breadcrumbs">
    <script type="text/javascript">
        try {
            ace.settings.check('breadcrumbs', 'fixed')
        } catch (e) {
        }
    </script>
    <ul class="breadcrumb" style="padding: 20px; color: #3a87ad;">
    </ul>
</div>


<div id="adEditBox"
     style="display: none; z-index: 99; width: 600px; position: fixed; left: 15%; top: 5%; border: 1px solid #ccc; border-radius: 4px !important;">
    <div class="arrow"></div>
    <div class="popover-inner" style="">
        <h3 class="popover-title" id="">编辑</h3>
        <div class="popover-content" id="" style="background-color: #F5F5F5;">
            <form class="form-horizontal" style="padding-top: 10px;"
                  method="get" id="adForm" enctype="multipart/form-data">
                <input type="hidden" id="id" name="id" value="" />

                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">签到天数：</label>
                    <div class="col-sm-9">
                        <input class="form-control" name="days" id="days"></input>
                    </div>
                    <span class="input_tip" style="line-height: 30px;">*</span>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">奖品详情：</label>
                    <div class="col-sm-9">
                        <input class="form-control" name="detail" id="detail"></input>
                    </div>
                    <span class="input_tip" style="line-height: 30px;">*</span>
                </div>
                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">库存：</label>
                    <div class="col-sm-9">
                        <input class="form-control"  type="number" name="number" id="number"></input>
                    </div>
                    <span class="input_tip" style="line-height: 30px;">*</span>
                </div>

                <div class="form-group">
                    <label for="" class="col-sm-2 control-label">概率(0,1]：</label>
                    <div class="col-sm-9">
                        <input class="form-control" name="changce" id="changce"></input>
                    </div>
                    <span class="input_tip" style="line-height: 30px;">*</span>
                </div>


            </form>
        </div>
        <div class="popover-footer"
             style="background-color: #F5F5F5; overflow: hidden; text-align: center;">
            <button type="button" class="btn btn-primary"
                    style="padding: 0px; margin: 0px 5px 5px 0px;" id="cancel">
                <i class="icon-ok"></i>取消
            </button>
            <button type="button" class="btn btn-danger"
                    style="padding: 0px; margin: 0px 5px 5px 0px;" id="save">
                <i class="icon-edit"></i>保存
            </button>

        </div>
    </div>
</div>


<div class="page-content">
    <div class="row" style="height: 100%">


        <div class="col-xs-12">
            <style>
                .table-responsive table th tr {
                    text-align: center;
                }
            </style>
            <div class="table-responsive">

                <input type="hidden" value="1" id="start"/>


                <table id="sample-table-2"
                       class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th style="text-align: center;">编号</th>
                        <th style="text-align: center;">等级</th>
                        <th style="text-align: center;">详情</th>
                        <th style="text-align: center;">条件(签到天数)</th>
                        <th style="text-align: center;">概率(0~1]</th>
                        <th style="text-align: center;">被抽中次数</th>
                        <th style="text-align: center;">库存</th>
                        <th style="text-align: center;">最近更新时间</th>
                        <th style="text-align: center;">操作</th>

                    </tr>
                    </thead>
                    <tbody id="data-list" style="text-align: center;">
                    </tbody>
                </table>
            </div>
        </div>
        <!-- /.col -->

    <div class="row" >
        <div class="col-xs-12">
        修改注意事项:
      <ol>
          <li>一等奖需要的签到天数> 二等奖 > 三等奖</li>
          <li>库存为0的意思 就是不会被抽中</li>
          <li>概率为0~1 间的小数</li>
          <li>签到天数>0</li>
      </ol>
            </div>
    <div>

    </div>
</div>
<div id="loading"
     style="width: 32px; height: 32px; position: fixed; left: 40%; top: 40%; display: none;">
    <img src="<%=contextPath%>/admin/assets/css/images/loading.gif"
         width="32" height="32"/>
</div>
<div class="popover"
     style="z-index: 9999; width: 150px; position: fixed; left: 40%; top: 40%;">
    <div class="arrow"></div>
    <div class="popover-inner">
        <h3 class="popover-title">操作提示</h3>
        <div class="popover-content" id="popover-content">
            <p>操作成功</p>
        </div>
    </div>
</div>

<script type="text/javascript">
    if ("ontouchend" in document)
        document
                .write("<script src='<%=contextPath%>/admin/assets/js/jquery.mobile.custom.min.js'>" + "<" + "/script>");
</script>
<script type="text/javascript">
    window.jQuery
    || document
            .write("<script src='<%=contextPath%>/admin/assets/js/jquery-2.0.3.min.js'>"
                    + "<" + "script>");
</script>

<!--[if IE]>
<script type="text/javascript">
    window.jQuery || document.write("<script src='<%=contextPath%>/admin/assets/js/jquery-1.10.2.min.js'>" + "<" + "script>");
</script>
<![endif]-->
<script type="text/javascript">
    if ("ontouchend" in document)
        document
                .write("<script src='<%=contextPath%>/admin/assets/js/jquery.mobile.custom.min.js'>"
                        + "<" + "script>");
</script>
<script src="<%=contextPath%>/admin/assets/js/bootstrap.min.js"></script>
<script src="<%=contextPath%>/admin/assets/js/typeahead-bs2.min.js"></script>

<!--[if lte IE 8]>
<script src="assets/js/excanvas.min.js"></script>
<![endif]-->
<script
        src="<%=contextPath%>/admin/assets/js/jquery-ui-1.10.3.custom.min.js"></script>
<script
        src="<%=contextPath%>/admin/assets/js/jquery.ui.touch-punch.min.js"></script>
<script src="<%=contextPath%>/admin/assets/js/jquery.slimscroll.min.js"></script>
<script
        src="<%=contextPath%>/admin/assets/js/jquery.easy-pie-chart.min.js"></script>
<script src="<%=contextPath%>/admin/assets/js/jquery.sparkline.min.js"></script>
<script src="<%=contextPath%>/admin/assets/js/ace-elements.min.js"></script>
<script src="<%=contextPath%>/admin/assets/js/ace.min.js"></script>
<script src="<%=contextPath%>/admin/js/common.js"></script>
<script src="<%=contextPath%>/admin/assets/js/jquery-form.js"></script>
<script
        src="<%=contextPath%>/admin/assets/js/jquery.dataTables.bootstrap.js"></script>
<script
        src="<%=contextPath%>/admin/assets/js/jquery.dataTables.bootstrap.js"></script>
<script
        src="<%=contextPath%>/admin/assets/js/date-time/bootstrap-datepicker.min.js"></script>


<script type="text/javascript">
    getData();

    function getData() {

        $("#loading").show();
        $.ajax({
            url: contextPath + '/m/attendance/giftList',
            type: "POST",
            dataType: 'json',
            success: function (rs) {

                if (rs.success) {
                    var data = rs.data;

                    var dataList = $("#data-list");
                    dataList.empty();

                    if (data.length > 0) {
                        parseData(data);
                    } else {
                        showMessage("无数据");
                    }
                } else {
                    showMessage("获取数据失败,请稍后再试");
                }
                $("#loading").hide();

            },
            error: function () {
                $("#loading").hide();
                showMessage("请稍后再试");
            }
        });
    }

    function parseData(data) {
        var dataList = $("#data-list");
        var tr = "";
        for (var i = 0; i < data.length; i++) {



            var dataitem = {
                id:data[i].id,
                detail:data[i].detail,
                days:data[i].days,
                changce:data[i].changce,
                number:data[i].number,
            }

            dataitem =  JSON.stringify(dataitem);



            tr += '<tr>';
            tr += '<td>' + data[i].id + '</td>';
            tr += '<td>' + data[i].name + '</td >';
            tr += '<td>' + data[i].detail + '</td>';
            tr += '<td>' + data[i].days + '</td>';
            tr += '<td>' + data[i].changce + '</td>';
            tr += '<td>' + data[i].sellcount + '</td>';
            tr += '<td>' + data[i].number + '</td>';
            tr += '<td>' + data[i].updateTime + '</td>';
            tr += '<td><button  datas= ' + dataitem + '  class="btn btn-primary btn-xs edit_btn">编辑</button></td>';
            tr += '</tr>';

        }

        dataList.append(tr);

        $(".edit_btn").bind("click",function () {

            $("#adEditBox").show();
            var data =  $(this).attr("datas");
            data = JSON.parse(data);
            $("#id").val(data.id);
            $("#days").val(data.days);
            $("#detail").val(data.detail);
            $("#number").val(data.number);
            $("#changce").val(data.changce);



        })
        

    }


    $("#cancel").bind("click", function () {
        $("#adEditBox").hide();
        $("#id").val("");
        $("#days").val("");
        $("#detail").val("");
        $("#number").val("");
        $("#changce").val("");




    });

    $("#save").bind("click", function () {

        var action='/m/attendance/changegift';



        if ($("#days").val() == "") {
            showMessage("请输入条件签到天数!!!");
            return;
        }
        if ($("#changce").val() == "") {
            showMessage("请输入概率!!!");
            return;
        }
        if ($("#number").val() == "") {
            showMessage("请输入库存!!!");
            return;
        }
        if ($("#detail").val() == "") {
            showMessage("请输入奖品内容!!!");
            return;
        }


        var s = $("#number").val();
        var p = $("#changce").val();

        if (!(/(^[0-9]\d*$)/.test(s)))
        {
            showMessage("库存必须为整数!!!");
            return false;
        }

        if(!(/^[-\+]?\d+(\.\d+)?$/).test(p)){
            showMessage("请输入正确的概率!!!");
            return false;
        }



        // S 必须为int  P必须为DOUBLE

        if (  Number(s)<0) {
            showMessage("库存必须>=0!!!");
            return ;
        }
        if (Number(p)>1 || Number(p) < 0 ){
            showMessage("概率必须在0~1之间！！！");
            return ;
        }



        //  0<概率<=1 要
        //  条件签到天数  一等奖大于二等奖大于三等奖
        //  库存>=0





        $("#detail").val(encodeURI($("#detail").val()));




        $("#loading").show();
        $("#adForm").ajaxSubmit({
            type: "GET",
            url: contextPath + action,
            dataType: 'json',
            success: function (data) {
                if (data.success) {
                    showMessage(data.msg);

                    $("#adEditBox").hide();
                    $("#id").val("");
                    $("#days").val("");
                    $("#detail").val("");
                    $("#number").val("");
                    $("#changce").val("");
                    setTimeout(function () {
                        getData(1);
                    }, 2000);
                } else {
                    showMessage(data.msg);
                }
                $("#loading").hide();
            },
            error: function (msg) {
                $("#loading").hide();
                showMessage("操作失败");
            }
        });
    });



</script>
</body>
</html>